//
//  CaoZuoDanQianShouCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "CaoZuoDanQianShouCell.h"
#import "LJCustomTextView.h"
#import "XLPhotoBrowser.h"

@interface CaoZuoDanQianShouCell ()<XLPhotoBrowserDelegate>

@property (nonatomic, strong) UIView *bgView1;
@property (nonatomic, strong) UILabel *noTitleLab;
@property (nonatomic, strong) UIView *noView;
@property (nonatomic, strong) UIView *bgView2;
@property (nonatomic, strong) UILabel *statusTitleLab;
@property (nonatomic, strong) UIView *statusBGView;
@property (nonatomic, strong) UIButton *normalBtn;
@property (nonatomic, strong) UIButton *abnormalBtn;
@property (nonatomic, strong) UIView *bgView3;
@property (nonatomic, strong) UILabel *personLab;
@property (nonatomic, strong) UILabel *lineLab;
@property (nonatomic, strong) UILabel *fileTitleLab;
@property (nonatomic, strong) UILabel *fileCountLab;
@property (nonatomic, strong) LJCustomTextView *fileTextView;
@property (nonatomic, strong) UIView *imgsView;
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation CaoZuoDanQianShouCell

+ (instancetype)CaoZuoDanQianShouCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"CaoZuoDanQianShouCell";
    CaoZuoDanQianShouCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[CaoZuoDanQianShouCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView1 = [UIView new];
        self.bgView1.layer.masksToBounds = YES;
        self.bgView1.layer.cornerRadius = 6;
        self.bgView1.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bgView1];
        
        self.noTitleLab = [UILabel new];
        self.noTitleLab.textColor = HexColor(0x2B2C44);
        self.noTitleLab.font = BoldFontOfSize(16);
        [self.bgView1 addSubview:self.noTitleLab];
        
        self.noView = [UIView new];
        self.noView.backgroundColor = HexColor(0xffffff);
        [self.bgView1 addSubview:self.noView];
        
        self.bgView2 = [UIView new];
        self.bgView2.layer.masksToBounds = YES;
        self.bgView2.layer.cornerRadius = 6;
        self.bgView2.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bgView2];
        
        self.statusTitleLab = [UILabel new];
        self.statusTitleLab.textColor = HexColor(0x2B2C44);
        self.statusTitleLab.font = [UIFont systemFontOfSize:16];
        [self.bgView2 addSubview:self.statusTitleLab];
        
        self.statusBGView = [UIView new];
        self.statusBGView.layer.masksToBounds = YES;
        self.statusBGView.layer.cornerRadius = 16;
        self.statusBGView.backgroundColor = HexColor(0xF2F2F5);
        [self.bgView2 addSubview:self.statusBGView];
        
        self.normalBtn = [UIButton new];
        self.normalBtn.layer.masksToBounds = YES;
        self.normalBtn.layer.cornerRadius = 12;
        [self.normalBtn setTitle:@"正常" forState:0];
        self.normalBtn.titleLabel.font = BoldFontOfSize(12);
        self.normalBtn.backgroundColor = Color_ThemeColor;
        [self.normalBtn setTitleColor:HexColor(0xffffff) forState:0];
        [self.normalBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
        [self.statusBGView addSubview:self.normalBtn];
        
        self.abnormalBtn = [UIButton new];
        self.abnormalBtn.layer.masksToBounds = YES;
        self.abnormalBtn.layer.cornerRadius = 12;
        [self.abnormalBtn setTitle:@"异常" forState:0];
        self.abnormalBtn.titleLabel.font = BoldFontOfSize(12);
        self.abnormalBtn.backgroundColor = HexColor(0xF2F2F5);
        [self.abnormalBtn setTitleColor:HexColor(0x585974) forState:0];
        [self.abnormalBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
        [self.statusBGView addSubview:self.abnormalBtn];
        
        self.bgView3 = [UIView new];
        self.bgView3.layer.masksToBounds = YES;
        self.bgView3.layer.cornerRadius = 6;
        self.bgView3.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bgView3];
        
        self.personLab = [UILabel new];
        self.personLab.textColor = HexColor(0x2B2C44);
        self.personLab.font = [UIFont systemFontOfSize:16];
        [self.bgView3 addSubview:self.personLab];
        
        self.lineLab = [UILabel new];
        self.lineLab.backgroundColor = Color_backgroundColor;
        [self.bgView3 addSubview:self.lineLab];
        
        self.fileTitleLab = [UILabel new];
        self.fileTitleLab.textColor = HexColor(0x2B2C44);
        self.fileTitleLab.font = [UIFont systemFontOfSize:14];
        self.fileTitleLab.text = @"附件备注";
        [self.bgView3 addSubview:self.fileTitleLab];
        
        self.fileCountLab = [UILabel new];
        self.fileCountLab.textColor = HexColor(0xBDBED9);
        self.fileCountLab.font = [UIFont systemFontOfSize:12];
        self.fileCountLab.text = @"0/255";
        [self.bgView3 addSubview:self.fileCountLab];
        
        self.fileTextView = [LJCustomTextView new];
        self.fileTextView.placeholder = @"请输入";
        self.fileTextView.textColor = HexColor(0x2B2C44);
        self.fileTextView.font = [UIFont systemFontOfSize:14];
        self.fileTextView.textLength = 255;
        __weak_self;
        self.fileTextView.countBlock = ^(NSString * _Nonnull str) {
            weakself.fileCountLab.text = str;
            if (weakself.remarkBlock) {
                weakself.remarkBlock(weakself.fileTextView.text);
            }
        };
        [self.bgView3 addSubview:self.fileTextView];
        
        self.imgsView = [UIView new];
        [self.bgView3 addSubview:self.imgsView];
        
        self.sureBtn = [UIButton new];
        self.sureBtn.layer.masksToBounds = YES;
        self.sureBtn.layer.cornerRadius = 6;
        self.sureBtn.backgroundColor = Color_ThemeColor;
        [self.sureBtn setTitle:@"确认签收" forState:0];
        [self.sureBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.sureBtn.titleLabel.font = BoldFontOfSize(16);
        [self.sureBtn setImage:[UIImage imageNamed:@"icon_adopt"] forState:0];
        self.sureBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.sureBtn];
    }
    return self;
}

- (void)sureClick {
    if (self.sureBlock) {
        self.sureBlock();
    }
}

- (void)changeClick {
    if (self.changeBlock) {
        self.changeBlock();
    }
}

-(void)setModel:(CaoZuoDanQianShouModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView1.nim_left = 15;
    self.bgView1.nim_top = 10;
    self.bgView1.nim_width = ScreenW - 30;
    
    self.noTitleLab.text = @"运单号";
    [self.noTitleLab sizeToFit];
    self.noTitleLab.nim_left = 15;
    self.noTitleLab.nim_top = 15;
    
    self.noView.nim_left = 15;
    self.noView.nim_top = self.noTitleLab.nim_bottom + 15;
    self.noView.nim_width = self.bgView1.nim_width - 30;
    
    [self.noView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat noWidth = self.noView.nim_width * 0.5;
    CGFloat noHeight = 17;
    __block CGFloat viewH = 0;
    [self.model.waybill_list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *lab = [UILabel new];
        lab.nim_left = ((idx + 1) % 2) == 0 ? 0 : noWidth;
        lab.nim_top = (idx / 2) * noHeight;
        lab.nim_width = noWidth;
        lab.nim_height = noHeight;
        lab.textColor = Color_ThemeColor;
        lab.font = [UIFont systemFontOfSize:14];
        lab.text = obj;
        [self.noView addSubview:lab];
        viewH = lab.nim_bottom;
    }];
    self.noView.nim_height = viewH;
    
    self.bgView1.nim_height = self.noView.nim_bottom + 15;
    
    self.bgView2.nim_left = self.bgView1.nim_left;
    self.bgView2.nim_top = self.bgView1.nim_bottom + 10;
    self.bgView2.nim_width = self.bgView1.nim_width;
    self.bgView2.nim_height = 50;
    
    self.statusTitleLab.text = @"签收状态";
    [self.statusTitleLab sizeToFit];
    self.statusTitleLab.nim_left = 15;
    self.statusTitleLab.nim_centerY = self.bgView2.nim_height * 0.5;
    
    self.statusBGView.nim_width = 96;
    self.statusBGView.nim_height = 32;
    self.statusBGView.nim_left = self.bgView2.nim_width - 15 - self.statusBGView.nim_width;
    self.statusBGView.nim_top = 9;
    
    self.normalBtn.nim_width = 44;
    self.normalBtn.nim_height = 24;
    self.normalBtn.nim_left = 4;
    self.normalBtn.nim_top = 4;
    
    self.abnormalBtn.nim_left = self.normalBtn.nim_right;
    self.abnormalBtn.nim_top = self.normalBtn.nim_top;
    self.abnormalBtn.nim_width = self.normalBtn.nim_width;
    self.abnormalBtn.nim_height = self.normalBtn.nim_height;
    
    if (self.model.isNormal) {
        self.normalBtn.backgroundColor = Color_ThemeColor;
        [self.normalBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.abnormalBtn.backgroundColor = HexColor(0xF2F2F5);
        [self.abnormalBtn setTitleColor:HexColor(0x585974) forState:0];
    }else {
        self.abnormalBtn.backgroundColor = HexColor(0xF35959);
        [self.abnormalBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.normalBtn.backgroundColor = HexColor(0xF2F2F5);
        [self.normalBtn setTitleColor:HexColor(0x585974) forState:0];
    }
    
    self.bgView3.nim_left = self.bgView1.nim_left;
    self.bgView3.nim_top = self.bgView2.nim_bottom + 10;
    self.bgView3.nim_width = self.bgView1.nim_width;
    
    self.lineLab.frame = CGRectMake(15, 60, self.bgView3.nim_width - 30, 1);
    
    self.personLab.text = [NSString stringWithFormat:@"签收人：%@",self.model.receipter];
    [self.personLab sizeToFit];
    self.personLab.nim_left = 15;
    self.personLab.nim_top = (self.lineLab.nim_top - self.personLab.nim_height) * 0.5;
    
    self.fileTitleLab.nim_left = self.personLab.nim_left;
    [self.fileTitleLab sizeToFit];
    self.fileTitleLab.nim_top = self.lineLab.nim_bottom + 20;
    
    self.fileCountLab.nim_left = self.fileTitleLab.nim_right + 10;
    [self.fileCountLab sizeToFit];
    self.fileCountLab.nim_bottom = self.fileTitleLab.nim_bottom;
    self.fileCountLab.nim_width += 50;
    
    self.fileTextView.frame = CGRectMake(self.fileTitleLab.nim_left, self.fileTitleLab.nim_bottom + 10, self.bgView3.nim_width - 2 * self.fileTitleLab.nim_left, 80);
    
    self.fileTextView.customText = self.model.remark.length?self.model.remark:@"";
    
    self.imgsView.frame = CGRectMake(15, self.fileTextView.nim_bottom + 15, self.bgView3.nim_width - 30, 90);
    CGFloat imgW = (self.imgsView.nim_width - 40) * 0.33333;
    self.imgsView.nim_height = imgW + 10;
    __block CGFloat viewLeft = 0;
    [self.imgsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.model.imgList enumerateObjectsUsingBlock:^(CaoZuoDanQianShouImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        btn.nim_left = (imgW + 20) * idx;
        btn.nim_top = 10;
        btn.nim_width = imgW;
        btn.nim_height = imgW;
        [btn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]] forState:0];
        btn.tag = idx;
        [btn addTarget:self action:@selector(imgPreView:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:btn];
        
        UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn.nim_right - 15, btn.nim_top - 7, 22, 22)];
        [deleteBtn setImage:[UIImage imageNamed:@"icon_caozuodan_delete_img"] forState:0];
        deleteBtn.tag = idx;
        [deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:deleteBtn];
        
        viewLeft = btn.nim_right + 20;
    }];
    
    if (self.model.imgList.count < 3) {
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(viewLeft, 10, imgW, imgW)];
        [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_caozuodan_upload_img"] forState:0];
        [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:addBtn];
    }
    
    self.bgView3.nim_height = self.imgsView.nim_bottom + 15;
    
    self.sureBtn.nim_left = self.bgView1.nim_left;
    self.sureBtn.nim_top = self.bgView3.nim_bottom + 20;
    self.sureBtn.nim_width = self.bgView1.nim_width;
    self.sureBtn.nim_height = 44;
    
    self.height = self.sureBtn.nim_bottom + 10;
}

- (void)imgPreView:(UIButton *)sender {
    NSMutableArray *mut = [NSMutableArray array];
    [self.model.imgList enumerateObjectsUsingBlock:^(CaoZuoDanQianShouImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mut addObject:[NSString stringWithFormat:@"%@%@",obj.url,obj.path]];
    }];
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:mut currentImageIndex:sender.tag];
    browser.browserStyle = XLPhotoBrowserStyleIndexLabel;//微博样式
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片", nil];
}

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    [browser saveCurrentShowImage];
}

- (void)deleteClick:(UIButton *)sender {
    if (self.deleteBlock) {
        self.deleteBlock(sender.tag);
    }
}

- (void)addClick {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
